<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {

/*	到本网站根目录下
	__ROOT__
	当前模块下当前控制器下
	__CONTROLLER__	
	到当前模块下
	__MODULE__ */

    public function index(){
        $this->assign('title','无足鸟');
        // 调用判断渲染方法,渲染登陆注册位置
        $this -> gongxianzhe();
        $this->display();
    }

    public function houduanjishu(){
        $this->assign('title','后端技术');
        // 调用判断渲染方法,渲染登陆注册位置
        $this -> gongxianzhe();
        $this->display();
    }

    public function qianduanjishu(){
        $this->assign('title','前端技术');
        // 调用判断渲染方法,渲染登陆注册位置
        $this -> gongxianzhe();
        $this->display();
    }

    public function zngg(){
        $this->assign('title','站内公告');
        // 调用判断渲染方法,渲染登陆注册位置
        $this -> gongxianzhe();
        $this->display();
    }

    public function mzsm(){
        $this->assign('title','免责声明');
        // 调用判断渲染方法,渲染登陆注册位置
        $this -> gongxianzhe();
        $this->display();
    }



    public function getmenu(){
        /*连接数据库*/
    	$ziyuan = M('ziyuan');

        if($_POST['page'] == 'houduanjishu') {
			$_POST['page'] = '后端技术';
		} else if($_POST['page'] == 'qianduanjishu'){
			$_POST['page'] = '前端技术';
		} else {
			$_POST['page'] = '网络技术';
		}
        $_POST['lan'] = htmlspecialchars($_POST['lan']);
		$result = $ziyuan -> query("select distinct zicaidan from ziyuan where caidan = ". "\"" . $_POST['lan'] . "\"" . " and jishu = " . "\"" . $_POST['page'] . "\" and shenhe = '1' ");
		$str = "";
		foreach ($result as $k => $v) {
            $str .= $v['zicaidan'] . ",";
        }
		$str = rtrim($str, ",");
		echo $str;		
	}

    public function getlist() {
        $ziyuan = M('ziyuan');
        /*处理ajax传过来的特殊字符*/
        if($_POST['menu'] == "C/C  /C#") { $_POST['menu'] = "C/C++/C#"; }

        if($_POST['page'] == 'houduanjishu') {
            $_POST['page'] = '后端技术';
        } else if($_POST['page'] == 'qianduanjishu'){
            $_POST['page'] = '前端技术';
        } else {
            $_POST['page'] = '网络技术';
        }
        $_POST['lan'] = htmlspecialchars($_POST['lan']);
        $_POST['menu'] = htmlspecialchars($_POST['menu']);
        $result = $ziyuan -> query("select ziyuanming from ziyuan where zicaidan = " . "\"" . $_POST['menu'] . "\"" . "and fenlei = '全部' and jishu = " . "\"" . $_POST['page'] . "\"" . " and caidan = ". "\"" . $_POST['lan'] . "\"" . " and shenhe = '1' ");
        $str = "";
        foreach ($result as $k => $v) {
            $str .= $v['ziyuanming'] . ",";
        }
        $str = rtrim($str, ",") . "^";
        $result = $ziyuan -> query($sql = "select ziyuan_id from ziyuan where zicaidan = " . "\"" . $_POST['menu'] . "\"" . "and fenlei = '全部' and jishu = " . "\"" . $_POST['page'] . "\"" . " and caidan = ". "\"" . $_POST['lan'] . "\"" . "and shenhe = '1' ");
        foreach ($result as $k => $v) {
            $str .= $v['ziyuan_id'] . ",";
        }
        $str = rtrim($str, ",");
        echo $str;
    }

    public function getinfo() {
        $ziyuan = M('ziyuan');
        $_POST['id'] = htmlspecialchars($_POST['id']);
        $result = $ziyuan -> query("select ziyuanming,shijian,gongxianzhe,lianjie from ziyuan where ziyuan_id = " . $_POST['id'] . " and shenhe = '1' ");
        $str = "";
        $str .= $result[0]["ziyuanming"] . ",";
        $str .= date('Y-m-d', $result[0]["shijian"]) . ",";
        $str .= $result[0]["gongxianzhe"] . ",";
        $str .= $result[0]["lianjie"];
        echo $str;
    }



    // 判断应该如何渲染前端模板中的 <{$gongxianzhe}> 占位符
    public function gongxianzhe(){
        if(session('name') == ''){
            $url_denglu = U('User/denglu');
            $url_zhuce = U('User/zhuce');
            $this -> assign('gongxianzhe','<a href="'.$url_denglu.'">登陆</a><a href="'.$url_zhuce.'">注册</a>');
        }else{
            $gongxianzhe = session('name');
            $url_gongxianzhe = U('User/fenxianziyuan');
            $url_zhuxiao = U('User/index');
            $this -> assign('gongxianzhe','<a href="'.$url_gongxianzhe.'">'.$gongxianzhe.'</a><a href="'.$url_zhuxiao.'">注销</a>');
        }
    }

}